﻿using System;
using System.Collections.Generic;
using System.Data;
using Rogan.Common;
using Rogan.Common.Model;
using Rogan.Dal;

// ReSharper disable once CheckNamespace
namespace Rogan.Bll
{
    //SysRole
    // ReSharper disable once PartialTypeWithSinglePart
    public partial class SysRoleBll
    {

        private readonly SysRoleDal _dal = new SysRoleDal();


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RoleID)
        {
            return _dal.Exists(RoleID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(SysRole model)
        {
            _dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SysRole model)
        {
            return _dal.Update(model);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RoleID)
        {

            return _dal.Delete(RoleID);
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string RoleIDlist)
        {
            return _dal.DeleteList(RoleIDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SysRole GetModel(int RoleID)
        {
            return _dal.GetModel(RoleID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public SysRole GetModelByCache(int RoleID)
        {

            string cacheKey = "Sys_RoleModel-" + RoleID;
            object objModel = DataCache.GetCache(cacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = _dal.GetModel(RoleID);
                    if (objModel != null)
                    {
                        int modelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, objModel, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
                    }
                }
                catch (Exception exception)
                {
                    LogHelper.WriteError("GetModelByCache", exception);
                }
            }
            return (SysRole)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            return _dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataTable GetList(int top, string strWhere, string filedOrder)
        {
            return _dal.GetList(top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SysRole> GetModelList(string strWhere)
        {
            DataTable dataTable = _dal.GetList(strWhere);
            return DataTableToList(dataTable);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SysRole> DataTableToList(DataTable dt)
        {
            List<SysRole> modelList = new List<SysRole>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                for (int n = 0; n < rowsCount; n++)
                {
                    SysRole model = _dal.DataRowToModel(dt.Rows[n]);

                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetAllList()
        {
            return GetList("");
        }
    }
}


